import _clonedeep from 'lodash/cloneDeep'
import util from '@/utils/utils'
export default {
  mixins: [util],
  props: {
    tableData: Array,
    selected: Array,
    operateParam: Object,
    pagination: Object,
    item: { type: Object,
      required: true
    }
  },
  data() {
    return {
      operateParam_: {
        isValid: true, // 是否校验
        autoSetParam: true, // 自动根据选中数据赋值
        reqRow: true, // 请求整行数据
        confFields: { // 审核字段
          last_chk_stcd: 'last_chk_stcd',
          last_chk_cnclsn_dsc: 'last_chk_cnclsn_dsc'
        },
        param: { // 请求参数

        }
      },
      idField: 'rcrd_id'
    }
  },
  methods: {
    validFailFn(msg, fn) {
      if (typeof fn === 'function') {
        fn(msg)
      } else {
        this.$message.warning(msg)
      }
      return false
    },
    /**
     * * @param datas {Array}
    *    @param validate {Array} [{
        key: 'Last_Chk_StCd',
        value: '2',
        msg: '只有[审核通过]的数据才能进行该操作',
        flag: true
      }]
         @param fn {Function}
    * */
    canOper(datas, validate, fn) {
      datas = Array.isArray(datas) ? datas : [datas]
      if (typeof validate === 'function') {
        return datas.every(data => {
          const msg = validate.call(this, data)
          if (msg && typeof msg === 'string') {
            this.$message.warning(msg)
            return false
          }
          return msg || true
        })
      }
      validate = validate || [{
        key: 'Last_Chk_StCd',
        value: '2',
        msg: '只有[审核通过]的数据才能进行该操作',
        flag: true
      }]
      return validate.every(({ key, value, msg, flag }) => {
        return datas.every(data => {
          const validValue = data[key]
          if (validValue === undefined) {
            return this.validFailFn(msg, fn)
          }
          const regResult = new RegExp(`^${value}$`).test(validValue)
          if (flag) {
            return regResult || this.validFailFn(msg, fn)
          } else {
            return !regResult || this.validFailFn(msg, fn)
          }
        })
      })
    },
    genParam() {
      const { selected, item, operateParam_, idField } = this
      const { param = {}, autoSetParam, reqRow, paramFilter } = operateParam_
      const urlParam = {}
      urlParam.pageType = item.type
      const selectedRow = selected[0] || {}
      Object.keys(param).forEach(key => {
        const paramValue = param[key]
        if (paramValue && !key.includes('grp')) {
          urlParam[key] = paramValue
          return true
        }
        if (key.includes('grp')) {
          const keyType = this.varType(paramValue)
          if (keyType === 'string') {
            const fields = paramValue.split(',')
            urlParam[key] = selected.reduce((pre, next) => {
              const data = {}
              for (const field of fields) {
                data[field] = next[field]
              }
              paramFilter && paramFilter(data)
              pre.push(data)
              return pre
            }, [])
          } else if (keyType === 'array') {
            urlParam[key] = paramValue
          } else {
            paramFilter && paramFilter(selected)
            urlParam[key] = selected
          }
        } else {
          if (!key.includes('grp') && selectedRow.hasOwnProperty(key)) {
            urlParam[key] = selectedRow[key]
          }
        }
      })
      if (autoSetParam) {
        const idValue = selectedRow[idField]
        if (idValue) {
          urlParam[idField] = idValue
        }
        if (reqRow) {
          Object.keys(selectedRow).forEach(key => {
            if (this.isNull(urlParam[key])) {
              urlParam[key] = selectedRow[key]
            }
          })
        }
      }
      return urlParam
    },
    canOpen() {
      return false
    },
    openPage() {
      const { url } = this.operateParam_
      const param = this.genParam()
      console.log('请求参数为：', param)
      // 判断是否需要跳转，高优先级
      if (url && typeof (url) === 'object') {
        const { path, name } = url
        if (path) {
          this.$router.push({
            path,
            query: param
          })
        } else if (name) {
          this.$router.push({
            name,
            params: param,
            query: param
          })
        }
      } else if (url && typeof (url) === 'string') {
        this.$router.push({
          path: url,
          query: param
        })
      }
    },
    validate() {
      const { selected, operateParam_: { isValid, validate }} = this
      return !isValid || this.canOper(selected, validate)
    },
    setParam(param = {}, flag = false) {
      if (flag) {
        this.operateParam_ = _clonedeep(param)
      } else {
        // Object.assign(this.operateParam_, param)
        this.assignData(param, this.operateParam_, true)
      }
    },
    getParam() {
      return this.operateParam_
    },
    setMnpltTpcd(mnplt_tpcd) {
      const _operateParam = this.getParam()
      if (_operateParam.param) {
        _operateParam.param.mnplt_tpcd = mnplt_tpcd
      } else {
        _operateParam.param = {
          mnplt_tpcd
        }
      }
    },
    handleClick() {
      const { operateParam_: { setParam, beforeFn }, operate, selected } = this
      if (beforeFn) {
        const result = beforeFn.call(this, selected, this)
        if (typeof result === 'string') {
          this.$warn(result)
          return
        }
        if (result === false) {
          return
        }
      }
      setParam && setParam.call(this, selected, this)
      operate.call(this)
    }
  },
  created() {
    this.setParam({ ...this.operateParam_, ...this.operateParam, ...this.item.operateParam }, true)
  }
}
